package com.tianyi.controller.menu;

import com.tianyi.controller.BaseController;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.MenuReq;
import com.tianyi.menu.MenuService;
import com.tianyi.model.SimpleLoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author cuixuhui
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    protected MenuService menuService;

    @RequestMapping(value = "loadMenu", method = RequestMethod.GET)
    public WebResult<?> loadMenu(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        Long empId = loginUser.getEmpId();

        return menuService.loadMenu(empId);//empId就是ID
    }

    @RequestMapping(value = "addMenu", method = RequestMethod.POST)
    public WebResult<?> addMenu(@RequestBody MenuReq req){
        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        handLoginUser(req);
        return menuService.addMenu(req);
    }

    @RequestMapping(value = "deleteMenu", method = RequestMethod.GET)
    public WebResult<?> deleteMenu(MenuReq req){
        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        handLoginUser(req);
        return menuService.deleteMenu(req);
    }

    @RequestMapping(value = "updateMenu", method = RequestMethod.POST)
    public WebResult<?> updateMenu(@RequestBody MenuReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        handLoginUser(req);
        return menuService.updateMenu(req);
    }

    @GetMapping(value = "queryMenuPage")
    public WebResult<?> queryMenuPage(MenuReq req){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        handLoginUser(req);
        return menuService.queryMenuPage(req);
    }

    @GetMapping(value = "getMenuPullDown")
    public WebResult<?> getMenuPullDown(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        return menuService.getMenuPullDown();
    }

    @RequestMapping(value = "loadMenuNewByRoleId", method = RequestMethod.GET)
    public WebResult<?> loadMenuNewByRoleId(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        Long roleId = loginUser.getRoleId();
        return menuService.loadMenuNewByRoleId(roleId);
    }

    @RequestMapping(value = "loadMenuNewByEmpId", method = RequestMethod.GET)
    public WebResult<?> loadMenuNewByEmpId(){

        SimpleLoginUser loginUser = getUserFromRequest();
        if (loginUser == null){
            return WebResult.error("login user is null");
        }
        Long empId = loginUser.getEmpId();
        return menuService.loadMenuNewByEmpId(empId);
    }
}
